
import Store from '@/modules/Store'
import Storage from '@/modules/Storage'
import Axios from '@/modules/Axios'

var userinfo = Storage.session('userinfo');
//回复认证信息
if(userinfo != null){
    Store.commit('user',JSON.parse(userinfo));
}

export default {
    user() {
        return Store.state.user;
    },
    check() {
        return Store.state.user.id > 0;
    },
    attempt(loginid,password) {
        return new Promise((resolve,reject) => {
            Axios.post('/auth/login',{
                loginid:loginid,password:password
            })
            .then((response) => {
                //设置Store
                Store.commit('user',response.data.contents);
                //设置Storage
                Storage.session('userinfo',JSON.stringify(response.data.contents));
                resolve(response.data);
            })
            .catch((error) => {
                reject(error.response);
            });
        });
    },
    logout() {
        Storage.removeSession('userinfo');
        Store.commit('defaultUser');
    }
}
